package com.dsteiner.xelavos.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import com.dsteiner.widgets.client.IJsUtil;
import com.dsteiner.widgets.client.JsUtil;
import com.dsteiner.widgets.client.Widgets.IHTML;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.controller.PopupOverlayController;
import com.dsteiner.widgets.client.domain.ModelObject;
import com.dsteiner.widgets.client.event.DataUpdateEvent;
import com.dsteiner.widgets.client.event.Event;
import com.dsteiner.widgets.client.event.EventListener;
import com.dsteiner.widgets.client.event.EventManager;
import com.dsteiner.widgets.client.model.PopupOverlayModel;
import com.dsteiner.xelavos.client.constants.Constants;
import com.dsteiner.xelavos.client.domain.Rating;
import com.dsteiner.xelavos.client.domain.TrackComments;
import com.dsteiner.xelavos.client.domain.User;
import com.dsteiner.xelavos.client.model.AuthenticationModel;
import com.dsteiner.xelavos.client.model.MyEntriesModel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

public class UserController {

	private IHTML titleHtml;
	private AuthenticationModel authenticationModel;
	private PopupOverlayModel popupOverlayModel;
	private IWidget myProfileWidget;
	private IWidget myEntriesWidget;
	private IHTML myEntriesLink;
	private ILazyWidget userWidget;
	private IJsUtil jsUtil;

	public void registerAuthenticationModel(AuthenticationModel authenticationModel) {
		this.authenticationModel = authenticationModel;
		authenticationModel.addPropertyChangeListener("user", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateVisibility();
				updateTitle();
				updateLinkVisibility();
			}
		});
	}

	public void registerEventManager(EventManager eventManager) {
		eventManager.registerListener(DataUpdateEvent.class, new EventListener(){
			public void onEvent(Event event) {
				DataUpdateEvent dataUpdateEvent = (DataUpdateEvent) event;
				if(dataUpdateEvent.getObject() instanceof User){
					updateTitle();
					updateLinkVisibility();
				}
			}
		});
	}

	public void registerLogoutLink(IHTML logoutLink) {
		logoutLink.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				authenticationModel.setUser(null);
				JsUtil.getInstance().trackGAVar(Constants.Util.getUserGAVar(null));
				authenticationModel.setRatings(new ArrayList<Rating>());
				authenticationModel.setTrackComments(new ArrayList<TrackComments>());
			}
		});
	}

	public void registerMyEntriesLink(final IHTML myEntriesLink) {
		this.myEntriesLink = myEntriesLink;
		myEntriesLink.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				jsUtil.trackEvent(Constants.CAT_ACCOUNT, Constants.ACT_CLICKED_MY_ENTRIES_LINK);
				popupOverlayModel.setContentWidget(myEntriesWidget);
				PopupOverlayController.showAtCenter(popupOverlayModel);
			}
		});
	}

	public void registerMyEntriesModel(final MyEntriesModel myEntriesModel) {
		myEntriesModel.addPropertyChangeListener("modelObjects", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				List<ModelObject> modelObjects = myEntriesModel.getModelObjects();
				if(modelObjects == null || modelObjects.isEmpty())
					popupOverlayModel.setVisible(false);
			}
		});
	}

	public void registerMyEntriesWidget(IWidget myEntriesWidget) {
		this.myEntriesWidget = myEntriesWidget;
	}

	public void registerMyProfileLink(final IHTML myProfileLink) {
		myProfileLink.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				jsUtil.trackEvent(Constants.CAT_ACCOUNT, Constants.ACT_CLICKED_MY_PROFILE_LINK);
				popupOverlayModel.setContentWidget(myProfileWidget);
				PopupOverlayController.showAtCenter(popupOverlayModel);
			}
		});
	}

	public void registerMyProfileWidget(IWidget myProfileWidget) {
		this.myProfileWidget = myProfileWidget;
	}

	public void registerPopupOverlayModel(PopupOverlayModel myProfilePopupModel) {
		this.popupOverlayModel = myProfilePopupModel;
	}

	public void registerTitleHtml(IHTML titleHtml) {
		this.titleHtml = titleHtml;
	}

	public void registerUserWidget(ILazyWidget userWidget) {
		this.userWidget = userWidget;
		userWidget.addWidgetListener(new WidgetListenerAdapter(){
			@Override
			public void onAttach(IWidget widget) {
				updateVisibility();
				updateLinkVisibility();
				updateTitle();
			}
		});
	}

	private void updateLinkVisibility() {
		User user = authenticationModel.getUser();
		if(user == null)
			return;
		myEntriesLink.setVisible(user.getEntriesCount() > 0);
	}

	private void updateTitle() {
		User user = authenticationModel.getUser();
		if(user == null)
			return;
		titleHtml.setHTML("Bienvenido "+user.getFirstName());
	}

	private void updateVisibility() {
		User user = authenticationModel.getUser();
		userWidget.setVisible(user != null);
	}

	public void registerJsUtil(IJsUtil jsUtil) {
		this.jsUtil = jsUtil;
	}

}
